import { createRouter, createWebHistory } from 'vue-router'
import MainLayout from '../layouts/MainLayout.vue'
import AdminLayout from '../layouts/AdminLayout.vue'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/userStore'

const routes = [
    // 1. 所有前台公共页面都使用 MainLayout 布局
    {
        path: '/',
        component: MainLayout,
        children: [
            {
                path: '',
                name: 'home',
                component: () => import('../views/HomePage.vue'),
            },
            {
                path: 'animal/:id',
                name: 'animalDetail',
                component: () => import('../views/AnimalDetailPage.vue')
            },
            {
                path: 'user/profile',
                name: 'userProfile',
                component: () => import('../views/UserProfilePage.vue'),
                meta: { requiresAuth: true }
            },
            {
                path: 'user/my-orders',
                name: 'myOrders',
                component: () => import('../views/MyOrdersPage.vue'),
                meta: { requiresAuth: true }
            },
            {
                path: 'user/create-order',
                name: 'CreateOrder',
                component: () => import('../views/CreateOrder.vue'),
                meta: { requiresAuth: true }
            },
            {
                path: 'order-receipt/:id',
                name: 'orderReceipt',
                component: () => import('../views/OrderReceipt.vue'),
                meta: { requiresAuth: true }
            }
        ]
    },

    // 2. 不需要任何布局的独立页面
    {
        path: '/login',
        name: 'login',
        component: () => import('../views/LoginPage.vue'),
        meta: { guest: true }
    },
    {
        path: '/register',
        name: 'register',
        component: () => import('../views/RegisterPage.vue'),
        meta: { guest: true }
    },

    // 3. 后台管理布局
    {
        path: '/admin',
        component: AdminLayout,
        meta: { requiresAuth: true, requiresAdmin: true },
        children: [
            { path: '', redirect: '/admin/dashboard' },
            { path: 'dashboard', name: 'adminDashboard', component: () => import('../views/admin/AdminDashboardPage.vue') },

            // 动物管理路由
            { path: 'animals', name: 'adminAnimalList', component: () => import('../views/admin/AdminAnimalListPage.vue') },
            { path: 'animals/add', name: 'adminAddAnimal', component: () => import('../views/admin/AdminAddAnimalPage.vue') },
            // 新增：编辑动物路由
            { path: 'animals/edit/:id', name: 'adminEditAnimal', component: () => import('../views/admin/AdminEditAnimalPage.vue') },

            // 用户管理路由
            { path: 'users', name: 'adminUserList', component: () => import('../views/admin/AdminUserListPage.vue') },
            { path: 'users/add', name: 'adminAddUser', component: () => import('../views/admin/AdminAddUserPage.vue') },
            // 新增：编辑用户路由
            { path: 'users/edit/:id', name: 'adminEditUser', component: () => import('../views/admin/AdminEditUserPage.vue') },

            { path: 'order-approval', name: 'adminOrderApproval', component: () => import('../views/admin/AdminOrderApprovalPage.vue') },
            {
                path: 'users/balance/:id', // 匹配我们菜单项的 index
                name: 'adminUserBalance',
                component: () => import('../views/admin/AdminUserMoney.vue') // 指向我们修改后的余额管理页
            },
        ]
    },

    // 4. 404 页面
    { path: '/:pathMatch(.*)*', name: 'NotFound', redirect: '/' }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
    const userStore = useUserStore();

    if (to.meta.requiresAuth && !userStore.isLoggedIn) {
        ElMessage.warning('请先登录');
        next({ name: 'login', query: { redirect: to.fullPath } });
        return;
    }
    if (to.meta.requiresAdmin && !userStore.isAdmin) {
        ElMessage.error('您没有权限访问该页面');
        next('/');
        return;
    }
    if (to.meta.guest && userStore.isLoggedIn) {
        next('/');
        return;
    }
    next();
});

export default router;